import re

from plugins import CheckPlugin
from config import logger
from helper import CallProcess, raw2lines

cmd = '/usr/bin/who -s'
logger.debug('check cmd: %s' % cmd)

class CheckLogged(CheckPlugin):
    topic = 'logged_user'
    # results = []

    def check(self):
        pat = re.compile(r'(\(|\))')
        proc = CallProcess(cmd)
        proc()
        lines = raw2lines(proc.stdout)
        for line in lines:
            li = [l.strip() for l in line.split()]
            self.results.append({'user': li[0],
                                 'pts': li[1],
                                 'from': pat.sub('', li[-1]),
                                 'time': ' '.join(li[2:-1])})


